<?php

namespace App\Http\Controllers\Kms\App\Album;

use App\Http\Controllers\BaseController;
use App\Http\Validate\Kms\Album\AlbumResourceValidate;
use App\Service\Kms\Album\AlbumResourceService;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;
use Illuminate\Validation\ValidationException;
use Psr\Container\ContainerExceptionInterface;
use Psr\Container\NotFoundExceptionInterface;

class AlbumResourceController extends BaseController
{

    /**
     * @param Request $request
     * @return JsonResponse
     * @throws ContainerExceptionInterface
     * @throws NotFoundExceptionInterface
     * @throws ValidationException
     */
    public function list(Request $request): JsonResponse
    {
        $params = AlbumResourceValidate::listValidate($request->all());
        return $this->success(AlbumResourceService::list($params));
    }

    /**
     * @param Request $request
     * @return JsonResponse
     * @throws ContainerExceptionInterface
     * @throws NotFoundExceptionInterface
     * @throws ValidationException
     */
    public function recycleBinList(Request $request): JsonResponse
    {
        $params = AlbumResourceValidate::recycleBinListValidate($request->all());
        return $this->success(AlbumResourceService::recycleBinList($params));
    }

    /**
     * @param Request $request
     * @return JsonResponse
     * @throws ValidationException
     * @throws ContainerExceptionInterface
     * @throws NotFoundExceptionInterface
     */
    public function upload(Request $request): JsonResponse
    {
        $params = AlbumResourceValidate::uploadValidate($request->all());
        return $this->success(AlbumResourceService::upload($params));
    }

    /**
     * @param Request $request
     * @return JsonResponse
     * @throws ContainerExceptionInterface
     * @throws NotFoundExceptionInterface
     * @throws ValidationException
     */
    public function joinRecycleBin(Request $request): JsonResponse
    {
        $params = AlbumResourceValidate::joinRecycleBinValidate($request->all());
        AlbumResourceService::joinRecycleBin($params);
        return $this->success();
    }

    /**
     * @param Request $request
     * @return JsonResponse
     * @throws ContainerExceptionInterface
     * @throws NotFoundExceptionInterface
     * @throws ValidationException
     */
    public function resetRecycleBin(Request $request): JsonResponse
    {
        $params = AlbumResourceValidate::resetRecycleBinValidate($request->all());
        AlbumResourceService::resetRecycleBin($params);
        return $this->success();
    }

    /**
     * @param Request $request
     * @return JsonResponse
     * @throws ContainerExceptionInterface
     * @throws NotFoundExceptionInterface
     * @throws ValidationException
     */
    public function batchResetRecycleBin(Request $request): JsonResponse
    {
        $params = AlbumResourceValidate::batchResetRecycleBinValidate($request->all());
        AlbumResourceService::batchResetRecycleBin($params);
        return $this->success();
    }

    /**
     * @param Request $request
     * @return JsonResponse
     * @throws ContainerExceptionInterface
     * @throws NotFoundExceptionInterface
     * @throws ValidationException
     */
    public function del(Request $request): JsonResponse
    {
        $params = AlbumResourceValidate::delValidate($request->all());
        AlbumResourceService::del($params);
        return $this->success();
    }

    /**
     * @param Request $request
     * @return JsonResponse
     * @throws ContainerExceptionInterface
     * @throws NotFoundExceptionInterface
     * @throws ValidationException
     */
    public function batchDel(Request $request): JsonResponse
    {
        $params = AlbumResourceValidate::batchDelValidate($request->all());
        AlbumResourceService::batchDel($params);
        return $this->success();
    }

    /**
     * @param Request $request
     * @return JsonResponse
     * @throws ValidationException
     */
    public function encodeUrl(Request $request): JsonResponse
    {
        $url = $request->get('resourceUrl', '');
        return $this->success(['url' => AlbumResourceService::genEncodeUrl($url)]);
    }

    /**
     * @param Request $request
     * @return JsonResponse
     */
    public function decodeUrl(Request $request): JsonResponse
    {
        try {
            $url = $request->get('resourceUrl', '');
            $url = urldecode($url);
            return $this->success(['url' => AlbumResourceService::decodeUrl($url)]);
        } catch (\Exception $exception) {
            return $this->fail(0, '链接获取失败，请检查密文是否正确');
        }

    }

    /**
     * @param Request $request
     * @return JsonResponse
     * @throws ContainerExceptionInterface
     * @throws NotFoundExceptionInterface
     * @throws ValidationException
     */
    public function resourceList(Request $request): JsonResponse
    {
        $params = AlbumResourceValidate::resourceListValidate($request->all());
        return $this->success(AlbumResourceService::resourceList($params));
    }

    /**
     * 运营相册
     * @param Request $request
     * @return JsonResponse
     */
    public function lists(Request $request): JsonResponse
    {
        $type                 = (int)$request->get('type', 0);                          //资源类型 1 - 图片 2 - 视频
        $isInRecycleBin       = (int)$request->get('isInRecycleBin', 0);                //是否加入回收站 0 - 否 1 - 是
        $creatorUsername      = (string)$request->get('creatorUsername', '');           //创建人名称
        $provinceCode         = (int)$request->get('provinceCode', 0);                  //省code
        $cityCode             = (int)$request->get('cityCode', 0);                      //市code
        $districtCode         = (int)$request->get('districtCode', 0);                  //区code
        $addressSuffix        = (string)$request->get('addressSuffix', '');             //地址后缀 - 详细地址
        $startTime            = (string)$request->get('startTime', '');                 //上传开始时间
        $endTime              = (string)$request->get('endTime', '');                   //上传结束时间
        $joinRecycleStartTime = (string)$request->get('joinRecycleStartTime', '');      //加入回收站开始时间
        $joinRecycleEndTime   = (string)$request->get('joinRecycleEndTime', '');        //加入回收站结束时间
        $rmsUserId            = (int)$request->get('rmsUserId', 0);                     //创建人id
        $pageSize             = (int)$request->get('pageSize', 20);
        $pageNum              = (int)$request->get('pageNum', 1);

        return $this->success(
            AlbumResourceService::lists(
                $isInRecycleBin, $type, $creatorUsername, $provinceCode, $cityCode, $districtCode, $addressSuffix,
                $startTime, $endTime, $joinRecycleStartTime, $joinRecycleEndTime, $rmsUserId, $pageSize, $pageNum
            )
        );
    }

    /**
     * 相册批量相关操作
     * @param Request $request
     * @return JsonResponse
     */
    public function batchOperate(Request $request): JsonResponse
    {
        $this->validate(
            $request, [
                'type' => 'required|integer|min:1',
                'ids'  => 'required|array|min:1',
            ]
        );
        $type = (int)$request->get('type', 0);   //1-批量加入回收站 2-批量恢复 3-批量删除 4-批量下载
        $ids  = (array)$request->get('ids', []); //批量操作的ids

        $userId = getRmsUserId();
        AlbumResourceService::batchOperate($ids, $type, $userId);
        return $this->success();
    }
}
